﻿using Twi.DataAccess.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Dynamic;
using Twi.COMMON.Core;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace Twi.Test.COMMON.Core
{
    
    
    /// <summary>
    ///这是 DACommonTest 的测试类，旨在
    ///包含所有 DACommonTest 单元测试
    ///</summary>
    [TestClass()]
    public class DACommonTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {

        }
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        //ORACLE 使用
        static string _OracleConnstr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.8)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));User Id=tabms;Password=tabms;";
        static DbUtility _OracleDbUtility = new DbUtility(_OracleConnstr, DbProviderType.Oracle);
        DACommon _OracleDACommon = new DACommon(_OracleDbUtility,"");

        //SQL SERVER 使用
        //static string _SQLConnStr ="Data Source=.\\SQLEXPRESS;Initial Catalog=GGT;Integrated Security=SSPI";
        static string _SQLConnStr = "Data Source=192.168.0.28;Initial Catalog=SDETicket2013DB_LYS;User Id=sa;Password=data-setpass;";
        static DbUtility _SQLDbUtility = new DbUtility(_SQLConnStr,DbProviderType.SqlServer);
        DACommon _SQLDACommon = new DACommon(_SQLDbUtility,"");

        /// <summary>
        ///SimpleNew 的测试
        ///</summary>
        [TestMethod()]
        public void SimpleNewTest()
        {
            dynamic dy = new
            {
                TableName = "WEB_SYS_ROLE",
                IDField ="ID",
                Fields =new {
                    RoleName="ID自动测试",
                    ID = "WEB_SYS_ROLE.ID",
                    //FID ="SEQ_User.nextval"
                },
                //Fields ="FID",
                MaxCodeFields="ID"
            };
            string data = JSSerialize.Serialize(dy);
            //_OracleDACommon.SimpleNew(data);
            _SQLDACommon.MaxCode = new DBMaxCode();
            _SQLDACommon.MaxCode.DB = _SQLDbUtility;
            _SQLDACommon.MaxCode.SignField = "TableName";
            _SQLDACommon.MaxCode.ValueField = "MaxRecID";
            _SQLDACommon.MaxCode.TableName = "Sys_MaxRecId";
            string actual = _SQLDACommon.SimpleNew(data);
            Assert.AreEqual("6", actual);
            //Assert.Inconclusive("验证此测试方法的正确性。");
        }

        /// <summary>
        ///GetPagingDataByCondition 的测试
        ///</summary>
        [TestMethod()]
        public void GetPagingDataByConditionTest()
        {
            string tableName = "TUsers";
            Dictionary<string, object> dictCondition =  new Dictionary<string, object>();
            dictCondition.Add("FPwd", "202CB962AC59075B964B07152D234B70");
            SimpleCondition simpleCondition = null; // TODO: 初始化为适当的值
            string selectFields = "*"; // TODO: 初始化为适当的值
            string orderFields = "FSort"; // TODO: 初始化为适当的值
            PagingCondition paging =  new PagingCondition();
            paging.needPaging = true;
            paging.pageSize = 10;
            paging.startIndex = 0;
            SimpleCondition treeCondition = null; // TODO: 初始化为适当的值           
            DataTable actual;
            actual = _SQLDACommon.GetPagingDataByCondition(tableName, dictCondition, simpleCondition, selectFields, orderFields, paging, treeCondition);
            Assert.AreEqual("17", actual.TableName);
            //Assert.Inconclusive("验证此测试方法的正确性。");
        }

        /// <summary>
        ///NewOrEdit 的测试
        ///</summary>
        [TestMethod()]
        public void NewOrEditTest()
        {
            dynamic dy = new
            {
                TableName = "WEB_SYS_ROLE",
                IDField = "ID",
                NewOrEdit = "New",
                Fields = new
                {
                    RoleName = "ID自动测试",
                    ID = "WEB_SYS_ROLE.ID",
                    //FID ="SEQ_User.nextval"
                },
                //Fields ="FID",
                MaxCodeFields = "ID"
            };
            string data = JSSerialize.Serialize(dy);
            //_OracleDACommon.SimpleNew(data);
            _SQLDACommon.MaxCode = new DBMaxCode();
            _SQLDACommon.MaxCode.DB = _SQLDbUtility;
            _SQLDACommon.MaxCode.SignField = "TableName";
            _SQLDACommon.MaxCode.ValueField = "MaxRecID";
            _SQLDACommon.MaxCode.TableName = "Sys_MaxRecId";
            string actual = _SQLDACommon.NewOrEdit(data);
            Assert.AreEqual("8", actual);
            //Assert.Inconclusive("验证此测试方法的正确性。");
        }

        /// <summary>
        ///GetPagingData 的测试
        ///</summary>
        [TestMethod()]
        public void GetPagingDataTest()
        {

            DbUtility db = _SQLDbUtility;
            DACommon target = new DACommon(db);
            string strSql = "select * from Tkt_TradeMain";
            IList<DbParameter> dbParams = null;
            PagingCondition paging = new PagingCondition();
            paging.orderByFields = "ID";
            paging.startIndex = 10;
            DataTable actual;
            actual = target.GetPagingData(strSql, dbParams, paging);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
    }
}
